//
//  AssistantLeftTableViewCell.m
//  markmap
//
//  Created by 甘世清 on 2024/4/29.
//

#import "AssistantLeftTableViewCell.h"

@implementation AssistantLeftTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)SQ_Style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
   
    [self.contentView addSubview:self.avatarImageView];
    [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(10);
        make.size.mas_equalTo(48);
        make.left.equalTo(self.contentView).offset(16);
    }];
   
    
    UIView * bgView = [[UIView alloc]init];
    bgView.layer.cornerRadius = 20;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = SQHexColor(@"#FFFFFF", 0.5);
    [self.contentView addSubview:bgView];
    
    [self.contentView addSubview:self.messageTextView];
    [self.messageTextView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.avatarImageView).offset(8);
            make.left.equalTo(self.avatarImageView.mas_right).offset(18);
    }];
    
//    UILongPressGestureRecognizer * rec = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(SQ_btn)];
//    rec.minimumPressDuration = 0.8;
//    [self.contentView addGestureRecognizer:rec];
//    
    WS(weakSelf);
    self.messageTextView.showTextMenu = ^(CGRect startRect, CGRect endRect, NSRange selectRange, BOOL isSelectAll) {
        [weakSelf showMenuWithStartRect:startRect endRect:endRect isAll:isSelectAll];
    };
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.messageTextView).insets(UIEdgeInsetsMake(-6, -10, 0, -6));
    }];
    UIView * hudView = [[UIView alloc]init];
    hudView.backgroundColor = UIColor.clearColor;
    [self.contentView addSubview:hudView];
    [hudView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_right);
        make.centerY.equalTo(bgView);
        make.size.mas_equalTo(34);
    }];
    [hudView addSubview:self.hud];
    
//
    self.FS_cellBottomView = self.messageTextView;
}
-(void)SQ_btn{
    [self showMenuWithStartRect:CGRectZero endRect:CGRectZero isAll:NO];
}

- (void)setModel:(MessageModel *)model{
    _model = model;
    self.avatarImageView.image = SQGetImage(@"logo");
    self.messageTextView.text = model.content;
    if (model.type ==1) {
        [self.hud startAnimating];
    } else {
        [self.hud stopAnimating];
    }
    [self.messageTextView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(model.cellSize.width);
        make.height.mas_equalTo(model.cellSize.height);
    }];
    [self.contentView layoutIfNeeded];
}

- (void)showMenuWithStartRect:(CGRect)startRect endRect:(CGRect)endRect isAll:(BOOL)isAll
{
    // 显示菜单栏
    SJPopMenuItem *copy = [SJPopMenuItem itemWithType:SJPopMenuItemCopy];
    SJPopMenuItem *selectAll = [SJPopMenuItem itemWithType:SJPopMenuItemSelectAll];
    NSMutableArray *items = [NSMutableArray array];
    [items addObject:copy];//复制
    if (!isAll) {
        [items addObject:selectAll]; //全选
    }
    SJPopMenu *menu = [SJPopMenu menu];
    if (isAll) {
        [menu showBy:self.messageTextView withItems:items];
    } else {
        [menu showBy:self.messageTextView startRect:startRect endRect:endRect withItems:items];
    }
    __weak typeof(self) weakSelf = self;
    menu.itemActions = ^(SJPopMenuItemType type, NSString *title) {
        switch (type) {
            case SJPopMenuItemCopy://复制
                [weakSelf coppy];
                break;
            case SJPopMenuItemWithdraw: //撤回
                
                break;
            case SJPopMenuItemDelete: //删除
                
                break;
            case SJPopMenuItemSelectAll: //全选
                [weakSelf selectAllText];
                break;
            default:
                break;
        }
    };
}

- (void)coppy
{
    UIPasteboard *pab = [UIPasteboard generalPasteboard];
      pab.string = [_model.content substringWithRange:self.messageTextView.selectedRange];
      if (pab == nil) {
          [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"复制失败")];
      } else {
          [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"复制成功")];
      }
 
}

- (void)selectAllText
{
   NSLog(@"%s", __func__);
  self.messageTextView.selectedRange = NSMakeRange(0, self.messageTextView.text.length);
}


- (UIImageView *)avatarImageView{
    if (!_avatarImageView) {
        _avatarImageView = [[UIImageView alloc]init];
        _avatarImageView.clipsToBounds = YES;
        _avatarImageView.layer.cornerRadius = 24;
        _avatarImageView.layer.masksToBounds = YES;
        _avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _avatarImageView;
}
- (UILabel *)messageLabel{
    if (!_messageLabel) {
        _messageLabel = [[UILabel alloc]init];
        _messageLabel.font = FONT_MEDIUM(15);
        _messageLabel.textColor = SQColor333333;
        _messageLabel.backgroundColor = UIColor.clearColor;
        _messageLabel.numberOfLines = 0;
        _messageLabel.lineBreakMode = NSLineBreakByClipping;
     }
    return _messageLabel;
}

- (SJCustomSelectTextView *)messageTextView{
    if (!_messageTextView) {
        _messageTextView = [[SJCustomSelectTextView alloc]init];
        _messageTextView.backgroundColor = UIColor.clearColor;
//        _messageTextView.layer.cornerRadius = 20;
//        _messageTextView.layer.masksToBounds = YES;
        _messageTextView.font = FONT_MEDIUM(15);
        _messageTextView.textColor = SQColor333333;
        _messageTextView.editable = NO;
        _messageTextView.selectable = YES;
//        _messageTextView.userInteractionEnabled = NO;
    }
    return _messageTextView;
}

- (UIActivityIndicatorView *)hud{
    if (!_hud) {
        _hud = [[UIActivityIndicatorView  alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium];
        _hud.hidesWhenStopped = YES;
        _hud.center = CGPointMake(17, 17);
        _hud.color = UIColor.whiteColor;
    }
    return _hud;
}
@end
